package com.ilink.teacherservice.service.impl.bailian;

import com.aliyun.bailian20231229.models.RetrieveResponse;
import com.aliyun.tea.TeaException;
import com.ilink.teacherservice.service.bailian.RetrieveService;
import org.springframework.stereotype.Service;

@Service
public class RetrieveServiceImpl implements RetrieveService {

    /**
     * 返回格式
     * "{\"Status\":200,\"RequestId\":\"45EB4E24-0C59-5599-80D9-717503918010\",\"Message\":\"success\",
     * \"Data\":{\"Nodes\":[{\"Score\":0.5558185577392578,
     * \"Metadata\":{\"parent\":\"Optimization trajectory. Besides understanding natural language instructions, LLMs are also shown to be able to recognize patterns from in-context demonstrations(Wei et al., 2023; Madaan&Yazdanbakhsh,\\net al., 2023; Madaan&Yazdanbakhsh, 2022; Mirchandani et al., 2023). Our meta-prompt makes use of this property and instructs the LLM to leverage the optimization trajectory for generating new\\nthe optimization trajectory for generating new solutions. Specifically, the optimization trajectory includes past solutions paired with their optimization scores, sorted in the ascending order.\\nscores, sorted in the ascending order. Including optimization trajectory in the meta-prompt allows the LLM to identify similarities of solutions with high scores, encouraging the LLM to build upon\",
     * \"file_path\":\"https://bailian-datahub-data-prod.oss-cn-beijing.aliyuncs.com/multimodal/docJson/10249160/%E7%94%A8%E5%A4%A7%E6%A8%A1%E5%9E%8B%E6%89%BEprompt2309.03409_1723185227903.json?Expires=1723195643&OSSAccessKeyId=LTAI5tKzNnKPFwCJSCpxx51h&Signature=CbxnYX%2BB7KTGXzmygl1jkEYg0rY%3D\",
     * \"is_displayed_chunk_content\":true,\"image_url\":[],\"nid\":\"38d477901ffa39c2aa8d42f91858a2a9\",
     * \"_q_score\":0.5966879399956723,\"title\":\"LARGE LANGUAGE MODELS AS OPTIMIZERS 2.2 META-PROMPT DESIGN\",
     * \"_score\":0.5558185577392578,\"doc_id\":\"file_e9a99f95147b4289888c766046369f1f_10249160\",
     * \"content\":\"the optimization trajectory for generating new solutions. Specifically, the optimization trajectory includes past solutions paired with their optimization scores, sorted in the ascending order.\",
     * \"workspace_id\":\"llm-r45cwcx502bjmuhk\",\"_rc_score\":1.0,\"hier_title\":\"LARGE LANGUAGE MODELS AS OPTIMIZERS>2.2 META-PROMPT DESIGN\",
     * \"_rc_t_score\":37.176228,\"doc_name\":\"用大模型找prompt2309.03409\",\"pipeline_id\":\"0eni7abzcn\",
     * \"_id\":\"llm-r45cwcx502bjmuhk_0eni7abzcn_file_e9a99f95147b4289888c766046369f1f_10249160_0_89\"},
     * \"Text\":\"the optimization trajectory for generating new solutions. Specifically, the optimization trajectory includes past solutions paired with their optimization scores, sorted in the ascending order.\"}
     **/
    @Override
    public RetrieveResponse retrieve(com.aliyun.bailian20231229.Client client, String query, Integer denseSimilarityTopK, Boolean enableReranking, Boolean enableRewrite,
                                     String rerankModelName, Float rerankMinScore, Integer rerankTopN, String rewriteModelName,
                                     Integer sparseSimilarityTopK, String indexId, String workspaceId, Boolean saveRetrieverHistory) throws Exception {
        com.aliyun.bailian20231229.models.RetrieveRequest.RetrieveRequestRewrite rewrite0 = new com.aliyun.bailian20231229.models.RetrieveRequest.RetrieveRequestRewrite()
                .setModelName(rewriteModelName);
        com.aliyun.bailian20231229.models.RetrieveRequest.RetrieveRequestRerank rerank0 = new com.aliyun.bailian20231229.models.RetrieveRequest.RetrieveRequestRerank()
                .setModelName(rerankModelName);
        com.aliyun.bailian20231229.models.RetrieveRequest retrieveRequest = new com.aliyun.bailian20231229.models.RetrieveRequest()
                .setQuery(query)
                .setDenseSimilarityTopK(denseSimilarityTopK)
                .setEnableReranking(enableReranking)
                .setEnableRewrite(enableRewrite)
                .setRerank(java.util.Arrays.asList(
                        rerank0
                ))
                .setRerankMinScore(rerankMinScore)
                .setRerankTopN(rerankTopN)
                .setRewrite(java.util.Arrays.asList(
                        rewrite0
                ))
                .setSparseSimilarityTopK(sparseSimilarityTopK)
                .setIndexId(indexId);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        java.util.Map<String, String> headers = new java.util.HashMap<>();
        try {
            // 复制代码运行请自行打印 API 的返回值
            return client.retrieveWithOptions(workspaceId, retrieveRequest, headers, runtime);
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            //System.out.println(error.getMessage());
            // 诊断地址
            //System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            throw new TeaException(error.getMessage(), error);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            //System.out.println(error.getMessage());
            // 诊断地址
            //System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            throw new Exception(error.getMessage());
        }
    }

    @Override
    public RetrieveResponse retrieve(com.aliyun.bailian20231229.Client client, String query, String indexId, String workspaceId) throws Exception {
        /*
        com.aliyun.bailian20231229.models.RetrieveRequest.RetrieveRequestRewrite rewrite0 = new com.aliyun.bailian20231229.models.RetrieveRequest.RetrieveRequestRewrite()
                .setModelName("conv-rewrite-qwen-1.8b");
        */

        com.aliyun.bailian20231229.models.RetrieveRequest retrieveRequest = new com.aliyun.bailian20231229.models.RetrieveRequest()
                .setQuery(query)
                .setDenseSimilarityTopK(3)
                .setEnableReranking(false)
                //.setEnableRewrite(true)
                //.setRewrite(java.util.Arrays.asList(
                //        rewrite0
                //))
                //.setSparseSimilarityTopK(3)
                .setRerankTopN(3)
                .setIndexId(indexId);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        java.util.Map<String, String> headers = new java.util.HashMap<>();
        try {
            // 复制代码运行请自行打印 API 的返回值
            return client.retrieveWithOptions(workspaceId, retrieveRequest, headers, runtime);
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            //System.out.println(error.getMessage());
            // 诊断地址
            //System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            throw new TeaException(error.getMessage(), error);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            //System.out.println(error.getMessage());
            // 诊断地址
            //System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            throw new Exception(error.getMessage());
        }
    }
}
